package com.hh.admin.login.service;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.hh.common.ibatis.dao.IQueryDao;
import com.hh.common.util.MD5Util;

/**
 * 
 * @author 冯昌硕
 * @create 2012-12-6 下午8:52:11
 */
public class LoginService {
    
    private static final Logger log = Logger.getLogger(LoginService.class);
    
    private IQueryDao queryDao;
    
    /**
     * 后台管理员用户登录，判断登录账号密码是否正确
     * @param userName 登录帐号
     * @param password 登录密码
     * @return
     * @create 2012-12-17 下午9:32:09
     */
    public boolean checkUserLogin(String userName, String password) {
        boolean result = false;
        try {
            String srchedPwd = queryDao.queryForObject("user.selectPasswordByUserName", userName, String.class);
            log.debug("userName=[" + userName + "] 对应的查询密码 srchedPwd=[" + srchedPwd + "]");
            
            String md5Pwd = MD5Util.encryptStringByMD5(password);
            
            if(md5Pwd.equals(srchedPwd)) {
                result = true;
            }
            
        } catch (ClassCastException e) {
            log.error("后台管理登录ClassCastException异常：" + e.toString());
            e.printStackTrace();
        } catch (SQLException e) {
            log.error("后台管理登录SQLException异常：" + e.toString());
            e.printStackTrace();
        }
        
        return result;
    }

    public void setQueryDao(IQueryDao queryDao) {
        this.queryDao = queryDao;
    }
}
